import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JOptionPane;

import common.MyFile;

/**
 * 
 *  controls the Files - all of the files, of books purchased by user, can be downloaded to user's computer
 *
 */

public class MyBooksControl 
{
	/** holds data to be sent to the server */
	String[] purchased;
	
	/**
	 * initalize the varaiables
	 */
	public MyBooksControl(){}
	/**
	 *  returns all of the books purchased by the user 
	 * @return String[][]
	 */
	public String[][] getPurchased()
	{
		String temp;
		String[] res=null;
		String[] row=null;
		String[][] data=null;
		purchased=new String[3];
		purchased[0]=Main.gusers.id;
		purchased[1]="getpurchase";
		purchased[2]=Main.gusers.id;
		Main.log.connection.accept("purchase");
		Main.log.flag=false;
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if(temp.length()!=0)
		{
			res=temp.split("\\*");
			data=new String[res.length][7];
			for(int i=0;i<res.length;i++)
			{
				row=new String[7];
				row=res[i].split("\\#");
				String[] date=row[3].split("\\-");
				row[3]=date[2]+"-"+date[1]+"-"+date[0];
				for(int j=0;j<7;j++)
					data[i][j]=row[j];
			}
		}
		return data;
	}
	
	/** 
	 * downloads a selected file to the computer. this method uses the class "Specification" 
	 * to determine the format of the file and the destination to download the file to.
	 * @param serial book serial
	 * @param language book language
	 * @param pdf flag indicating if the file exist in pdf format
	 * @param Doc flag indicating if the file exist in doc format
	 * @param Fb flag indicating if the file exist in FB2 format
	 * @param name
	 */
	public void getFile(String serial,String language,String pdf,String Doc,String Fb,String name)
	{
		Boolean[] formats=new Boolean[3];
		for(int i=0;i<3;i++)
			formats[i]=false;
		if(pdf.equals("1"))
			formats[0]=true;
		if(Doc.equals("1"))
			formats[1]=true;
		if(Fb.equals("1"))
			formats[2]=true;
		Specification s=new Specification(Main.log.frame,name,serial,language,formats);
		s.setVisible(true);
		
	}
	
	/**
	 * does the actual transfer request from the server
	 * @param format file format
	 * @param serial book serial
	 * @param language book language
	 * @param dest destination to download the file
	 */
	public void transfer(String format,String serial,String language,String dest)
	{	
		purchased=new String[4];
		purchased[0]=Main.gusers.id;
		purchased[1]="getFile";
		purchased[2]=Main.gusers.id;
		purchased[3]=serial+language+"."+format;
		try {
			 String LocalfilePath=dest;
			 File newFile=new File(LocalfilePath);
			Main.log.connection.accept("purchase");
			Main.log.flag=false;
			while(Main.log.flag==false);
			Main.log.flag=false;
			MyFile file=ChatClient.getFileObj();
		
			 
			  
			  FileOutputStream fis=new FileOutputStream(newFile);
			  BufferedOutputStream bis = new BufferedOutputStream(fis);
			  bis.write(file.mybytearray,0,file.getSize());
			  fis.close();
			  if(JOptionPane.showConfirmDialog(null, "File Saved. View it Now?","Confirmation", 0) == 0)
			  {
				  Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+ dest);
			  }
	  	} 
	  catch (FileNotFoundException e) 
	  {
		  JOptionPane.showMessageDialog(null,"Invalid Destination Folder","ERROR",JOptionPane.ERROR_MESSAGE);
	  } catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	/**
	 * logs out of the system
	 */
	public void exit()
	{
		Main.myBooksGui.panel.setVisible(false);
		Main.myBooksGui.panel.setEnabled(false);
		Main.log.loginData=new String[3]; //GIL
		Main.log.loginData[0]=Main.gusers.id;
		Main.log.loginData[1]="logout";
		Main.log.loginData[2]=Main.gusers.id;
		Main.log.connection.accept("logout"); //GIL
		Main.log.flag=false;//GIL
		while(Main.log.flag==false);//GIL
		Main.log.flag=false;//GIL
		Main.log.user.setText("");
		Main.log.pass.setText("");
		Main.log.frame.setSize(650,550);
		Main.log.panel.setEnabled(true);
		Main.log.panel.setVisible(true);
	}
}
